<?php
/**
 * 充值
 * @param unknown_type $pMerBillNo
 * @return string
 */

function DoDpTrade($user_id,$platformNo,$pTrdAmt,$post_url){
	$pWebUrl=  "http://wap.yzlicai.com/index.php?m=Home&c=collocation&a=response_go&class_name=DoDpTrade&class_act=DoDpTrade&from=".strim($_REQUEST['from']);//web方式返回
	$uWebUrl =  "http://wap.yzlicai.com/member.php?ctl=uc_center";
	
	$pS2SUrl=  "http://wap.yzlicai.com/index.php?m=Home&c=collocation&a=notify&class_name=DoDpTrade&class_act=DoDpTrade&from=".strim($_REQUEST['from']);//s2s方式返回


	$user = array();
	$user = get_user_info("*","id = ".$user_id);

	//手续费
	$fee = 0;
	$fuioupay_log = array();
	$fuioupay_log['code'] = 'toRecharge';
	$fuioupay_log['create_date'] = to_date(TIME_UTC,'Y-m-d H:i:s');
	$GLOBALS['db']->autoExecute(DB_PREFIX."fuioupay_log",$fuioupay_log);
	$requestNo = $GLOBALS['db']->insert_id();

	$data = $Sign_data= array();
	$data['requestNo'] = $requestNo;
	$data['create_time'] = TIME_UTC;
	$data['user_id'] = $user_id;

	$Sign_data['mchnt_cd'] = $platformNo;
	$data['mchnt_txn_ssn'] = $Sign_data['mchnt_txn_ssn'] = $user['id'].time();
	$data['login_id'] = $Sign_data['login_id'] = $user['mobile'];//用户登录名
	$data['amt'] = $pTrdAmt - $fee;
	$Sign_data['amt'] = ($pTrdAmt- $fee)*100 ;
	$Sign_data['back_notify_url'] = $pS2SUrl;//商户后台通知地址
	$Sign_data['page_notify_url'] = $pWebUrl;//商户返回地址

	$data['feeMode'] = '';//
	$data['fee'] = $fee;
	$GLOBALS['db']->autoExecute(DB_PREFIX."fuioupay_recharge",$data,'INSERT');
	$id = $GLOBALS['db']->insert_id();

	ksort($Sign_data);
	foreach($Sign_data as $k => $v){
		if($k == 'page_notify_url'){
			$Sign_old .=$v;
		}else{
			$Sign_old .= $v."|";
		}
	}
	$pSign=rsaSign($Sign_old,PRIVATE_KEY_PATH);

	$strhtml = '';
	foreach($Sign_data as $k => $v){
		$strhtml .= '<input type="hidden" name="'.$k.'" value="'.$v.'" /><br/>';
	}

	$html = '<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8" /></head><body>
		<form name="form1" id="form1" method="post" action="'.$post_url.'" target="_self">
		<input type="hidden" name="signature" value="'.$pSign.'" />
		'.$strhtml.'
		<input type="hidden" value="提交" />
		<div style="width:100%;text-align:center;padding:50px 0;"><img src="'.APP_ROOT.'/public/loading.gif" />页面正在跳转，请稍后...</div>
		</form>
		</body></html>
		<script language="javascript">document.form1.submit();</script>';
	//

	$fuioupay_log = array();
	$fuioupay_log['html'] = $html;
	$GLOBALS['db']->autoExecute(DB_PREFIX."fuioupay_log",$fuioupay_log,'UPDATE','id='.$requestNo);
	return $html;

}


function DoDpTradeCallBack($str3Req){
	$mchnt_txn_ssn = $str3Req["mchnt_txn_ssn"];
	if($mchnt_txn_ssn){
		unset($str3Req["mchnt_txn_ssn"]);
	}

	if($str3Req["code"]=='0000'){

		$where = " mchnt_txn_ssn = '".$mchnt_txn_ssn."'";

		require_once(APP_ROOT_PATH."system/libs/user.php");


		$sql = "update ".DB_PREFIX."fuioupay_recharge set is_callback = 1 where is_callback = 0 and ".$where;
		$GLOBALS['db']->query($sql);
		if ($GLOBALS['db']->affected_rows()){

			$info = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."fuioupay_recharge where ".$where);
			/*
			if((float)$info['amt']!=0){
				modify_account(array('money'=>(float)$info['amt']),$info['user_id'],"托管充值",1,1);
			}
			

			$sdata['notice_sn'] = $sdata['outer_notice_sn'] = $info['mchnt_txn_ssn'];
			$sdata['money'] = $info['amt'];
			$sdata['create_time']=$info['create_time'];
			$sdata['pay_time'] = TIME_UTC;
			$sdata['create_date'] = $sdata['pay_date']  = to_date(TIME_UTC,"Y-m-d");
			$sdata['is_paid'] = 1;
			$sdata['user_id'] = $info['user_id'];
			$sdata['is_ips'] = 1;

			$GLOBALS['db']->autoExecute(DB_PREFIX."payment_notice",$sdata,"INSERT");
			*/


			//操作成功
			$data = array();
			$data['code'] = $str3Req["code"];//0000 成功 ，其他失败

			if (isset($str3Req['bizType']))
				$data['bizType'] = $str3Req["bizType"];

			if (isset($str3Req['message']))
				$data['message'] = $str3Req["message"];

			if (isset($str3Req['description']))
				$data['description'] = $str3Req["description"];


		}else{
			$data = array();
			$data['code'] = $str3Req["code"];//0000 成功 ，其他失败

			if (isset($str3Req['message']))
				$data['message'] = '更新is_callback=1失败';

			if (isset($str3Req['description']))
				$data['description'] = $str3Req["description"];

		}
		$GLOBALS['db']->autoExecute(DB_PREFIX."fuioupay_recharge",$data,'UPDATE',$where);
		return 1;
	}else{
		return 0;
	}
}

?>